package priv.dk.springboot.service.impl;

import org.springframework.stereotype.Service;
import priv.dk.springboot.dao.CategoryDao;
import priv.dk.springboot.domain.entity.Category;
import priv.dk.springboot.exception.BusinessException;
import priv.dk.springboot.service.CategoryService;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    private final CategoryDao categoryDao;

    public CategoryServiceImpl(CategoryDao categoryDao) {
        this.categoryDao = categoryDao;
    }

    @Override
    public List<Category> getAll() {
        return categoryDao.queryAll();
    }

    @Override
    public void addCategory(Category category) {
        int count = categoryDao.insert(category);
        if (count == 0) throw new BusinessException("添加失败");
    }

    @Override
    public void remove(Integer id) {
        int count = categoryDao.delete(id);
        if (count == 0) throw new BusinessException("删除失败");
    }

    @Override
    public void modify(Category category) {
        int count = categoryDao.update(category);
        if (count == 0) throw new BusinessException("修改失败");
    }
}
